package top.hkyzf.onekeycommand.mixin;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.world.ClientWorld;
import org.jetbrains.annotations.Nullable;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import top.hkyzf.onekeycommand.event.TickEvent;
import top.hkyzf.onekeycommand.util.Utils;

/**
 * description: 
 * @author zhufeng
 * @date 2022/9/7 16:35
 * @version V1.0
 */
@Mixin(MinecraftClient.class)
public class MinecraftClientMixin {

    @Shadow @Nullable public ClientWorld world;

    @Shadow @Nullable public ClientPlayerEntity player;

    @Inject(method = "tick", at = @At(value = "FIELD", opcode = Opcodes.GETFIELD,
            target = "Lnet/minecraft/client/MinecraftClient;currentScreen:Lnet/minecraft/client/gui/screen/Screen;",
            ordinal = 4, shift  = At.Shift.BY, by = -3))
    private void runTick(CallbackInfo ci) {
        TickEvent.Type type = this.player != null && this.world != null
                ? TickEvent.Type.IN : TickEvent.Type.OUT;
        TickEvent event = new TickEvent(type);
        Utils.LOGGER.info(event.toString());
    }
}
